package com.zre.controller;

import com.zre.model.vo.BookInfo;
import com.zre.service.BookInfoService;
import com.zre.util.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;



@RestController
public class BookInfoController extends BaseController{

    @Autowired
    private BookInfoService bookInfoService;


    /**
     * 查询数据分页显示
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "queryByConditions.do",  method = RequestMethod.GET)
    public String queryBooksByConditions(@RequestParam(value = "bookType", required = false) String bookType,
                                         @RequestParam(value = "bookName",required = false) String bookName,
                                         @RequestParam(value = "pageNum",required = false, defaultValue = "1") Integer pageNum,
                                         @RequestParam(value = "pageSize",required = false, defaultValue = "10") Integer pageSize){

        Integer start = (pageNum - 1) * pageSize;
        Integer end = pageNum * pageSize;

        PageUtils result = bookInfoService.queryBooksByCondition(bookType, bookName, start, end);

        result.setCurrentPage(pageNum);
        result.setPageSize(pageSize);

        return ok(result);
    }

    /**
     * 添加图书信息
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "addBooks.do", method = RequestMethod.POST)
    public Object addBook(BookInfo bookInfo) {

        bookInfoService.addBooks(bookInfo);
        return ok(null);
    }
}
